<?php

/* $Id$ */

// echo json_encode($_SERVER);exit;

require_once 'osapi/osapi.php';
require_once 'signature_method.php';
require_once 'config_consumer.php';

$_fk_s = isset($_GET['_fk_s']) ? $_GET['_fk_s'] : '';

if($_fk_s == 'orkut.com'){


    //Build a request object from the current request
    $request = OAuthRequest::from_request(null, null, array_merge($_GET, $_POST));

    //Initialize the new signature method
    $signature_method = new OrkutSignatureMethod();

    //Check the request signature
    $signature_valid = $signature_method->check_signature($request, null, null, $_GET["oauth_signature"]);

}elseif($_fk_s == 'myspace.com'){

    $params = array_merge($_GET, $_POST);

    //Build a request object from the current request
    $request = OAuthRequest::from_request(null, null, $params);

    //Initialize the new signature method
    $signature_method = new MyspaceSignatureMethod();

    //Check the request signature
    $consumer_setting = ConfigConsumer::get($_fk_s);
    $signature_valid = $signature_method->check_signature($request, new OAuthConsumer($consumer_setting['key'],$consumer_setting['secret']), null, $_GET["oauth_signature"]);


} else {
    echo json_encode('bad source');exit;
}


//Build the output object
$payload = array();
//Add extra parameters to help debugging
$payload["query"] = array_merge($_GET, $_POST);
$payload["rawpost"] = file_get_contents("php://input");

if ($signature_valid == true) {
    $payload["validated"] = true;
} else {
    $payload["validated"] = false;
    //Return the response as JSON
    echo json_encode($payload);exit;

}

// do my jobs below
$userId = $_GET['opensocial_viewer_id'];
$consumer_setting = ConfigConsumer::get($_fk_s);
$osapi = new osapi(new osapiMySpaceProvider(), new osapiOAuth2Legged($consumer_setting['key'],$consumer_setting['secret'], $userId));



$profile_fields = array(
    'id',
    'displayName',
);

// Start a batch so that many requests may be made at once.
$batch = $osapi->newBatch();

// Fetch the current user.
$self_request_params = array(
    'userId' => $userId,              // Person we are fetching.
    'groupId' => '@self',             // @self for one person.
    'fields' => $profile_fields       // Which profile fields to request.
);
$batch->add($osapi->people->get($self_request_params), 'self');

// Fetch the friends of the user
$friends_request_params = array(
    'userId' => $userId,              // Person whose friends we are fetching.
    'groupId' => '@friends',          // @friends for the Friends group.
    'fields' => $profile_fields,      // Which profile fields to request.
    'count' => 500,                   // Max friends to fetch.
);
$batch->add($osapi->people->get($friends_request_params), 'friends');

// Get supportedFields Request
$batch->add($osapi->people->getSupportedFields(), 'supportedFields');

// Send the batch request.
$result = $batch->execute();

$payload['result'] = $result;


//Return the response as JSON
echo json_encode($payload);exit;


